package com.hqyj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.*;
import com.hqyj.pojo.DayInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service//实现ManagerInfoService的接口功能
public class DayInfoServiceImpl implements DayInfoService {
    //创建一个 DayInfoDao的实现类对象
    @Autowired(required = false)
    DayInfoDao dayInfoDao;


    //根据管理员姓名，查询当前登录的管理员的日常安排
    @Override
    public HashMap<String, Object> selectUserSer(DayInfo day) {
        HashMap<String, Object> map=new HashMap<String, Object>();
        //设置分页参数
        PageHelper.startPage(day.getPage(),day.getRow());
        List<DayInfo> list =new ArrayList<>();
        //System.out.println("------"+day.getDayManager());
        list=dayInfoDao.selectUserSer(day);

        //把查询的数据转换成分页对象
        PageInfo<DayInfo> page = new PageInfo<DayInfo>(list);
        //获取分页的当前页的集合
        map.put("list",page.getList());
        //总条数
        map.put("total",page.getTotal());
        //总页数
        map.put("totalPage",page.getPages());
        //上一页
        if(page.getPrePage()==0){
            map.put("pre",1);
        }else{
            map.put("pre",page.getPrePage());
        }
        //下一页
        //判断下一页的值是否大于等于最后一页
        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());
        return map;
    }

    //标记完成
    @Override
    public String tag(DayInfo day) {
        int num = dayInfoDao.tag(day);
        if(num>0){
            return "标记完成";
        }
        return "标记失败";
    }

    //删除
    @Override
    public String del(DayInfo day) {
        int num = dayInfoDao.del(day);
        if(num>0){
            return "删除成功";
        }
        return "删除失败";
    }

    //新增
    @Override
    public String dailyAdd(DayInfo day) {
        //获取时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today=sdf.format(new Date());
        day.setDayTime(today);
        day.setDayFinish("否");
        int num = dayInfoDao.dailyAdd(day);
        if(num>0){
            return "新增成功";
        }
        return "新增失败";
    }
}
